package com.midian.service.organization.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.midian.common.dao.BaseDaoTemplet;
import com.midian.common.dao.OrderHelper;
import com.midian.common.dao.SearchHelper;
import com.midian.common.dao.SearchHelper.Type;
import com.midian.common.exception.BizException;
import com.midian.common.pager.Pager;
import com.midian.common.system.SysStatic;
import com.midian.model.organization.Organization;
import com.midian.service.common.BaseService;
import com.midian.service.organization.IOrganizationService;

/**
 * 机构业务实现类
 * @author Mengfh
 *
 */
@Service("organizationService")
public class OrganizationServiceImpl extends BaseService implements IOrganizationService {
	/**机构DAO*/
	private BaseDaoTemplet<Organization> organizationDAO;
	
	@Resource(name = "organizationDAO")
	public void setOrganizationDAO(BaseDaoTemplet<Organization> organizationDAO) {
		this.organizationDAO = organizationDAO;
	}

	@Override
	public Organization add(Organization target) throws BizException {
		this.organizationDAO.add(target);
		return target;
	}

	@Override
	public void update(Organization target) throws BizException {
		this.organizationDAO.updateByConvertHql(target);
	}

	@Override
	public void delete(Serializable id) throws BizException {
		this.organizationDAO.delete(id);
	}

	@Override
	public void delete(Serializable[] ids) throws BizException {
		this.organizationDAO.delete(ids);
	}

	@Override
	public Organization get(Serializable id) throws BizException {
		return this.organizationDAO.get(id);
	}

	@Override
	public List<Organization> showList(Organization target, Pager pager, List<OrderHelper> helper) throws BizException {
		List<Organization> orgList = this.organizationDAO.find(target, getSearchHelper(), helper, pager);
		return orgList;
	}

	@Override
	public void opDelFlag(Serializable[] ids, Integer status) throws BizException {
		this.organizationDAO.opDelFlag(ids, status);
	}

	@Override
	protected List<SearchHelper> getSearchHelper() {
		
		String[] attrs = {"orgType","logo","name","contactPerson","contactNum","contactAddress","delFlag","orgType.parentId"};
		Type[] types = {Type.LEFTFETCH,Type.LEFTFETCH,Type.LIKE,Type.LIKE,Type.EQ,Type.LIKE,Type.EQ,Type.EQ};
		List<SearchHelper> searchList = SearchHelper.getSearchList(attrs, types);
		return searchList;
	}

	@Override
	public List<Organization> getOrginzations() {
		String hql = "from Organization t where t.delFlag = ?";
		return  this.organizationDAO.find(hql, new Object[]{SysStatic.NORMALDELFLAG});
	}
}
